package hyl.core.net;

import com.sun.mail.util.MailSSLSocketFactory;

import hyl.core.MyFun;
import hyl.core.conf.MyConfig;

import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

public class MyMail {
//	private static final Logger logger = LoggerFactory.getLogger(MyMail.class);
	// 用于给用户发送邮件的邮箱

	// 邮箱的用户名
	private String username = "账号@qq.com";
	// 邮箱的密码
	private String password = "授权码";
	// 发送邮件的服务器地址
	private String host = "smtp.qq.com";
	private boolean ssl = false;
	private boolean debug = false;
	static Map<String, MyMail> _mss = new HashMap<>();

	public static MyMail getInstance(String 账号) {
		if (_mss.containsKey(账号))
			return _mss.get(账号);
		Map<String, Map<String, String>> mailconf = MyConfig.getConfOfMail();
		if (mailconf == null)
			return null;
		Map<String, String> curconf = mailconf.get(账号);
		MyMail ms = new MyMail(账号, curconf);
		_mss.put(账号, ms);
		return ms;

	}

	private MyMail(String 账号, Map<String, String> curconf) {
		host = curconf.get("smtp");
		ssl = MyFun.str2Bool(curconf.get("ssl"));
		username = curconf.get("user");
		password = curconf.get("pass");
		debug = MyFun.str2Bool(curconf.get("debug"));
	}

	// 重写run方法的实现，在run方法中发送邮件给指定的用户
	public void send(String 收件人, String 主题, String 内容) {
		send(收件人, null, 主题, 内容);
	}

	public void send(String 收件人, String 抄送, String 主题, String 内容) {
		try {
			Properties prop = new Properties();
			prop.setProperty("mail.host", host);
			prop.setProperty("mail.transport.protocol", "smtp");
			prop.setProperty("mail.smtp.auth", "true");
			Session session = null;
			// 关于QQ邮箱，还要设置SSL加密，加上以下代码即可
			if (ssl) {
				MailSSLSocketFactory sf = new MailSSLSocketFactory();
				sf.setTrustAllHosts(true);
				prop.put("mail.smtp.ssl.enable", "true");
				prop.put("mail.smtp.ssl.socketFactory", sf);

				// 1、创建定义整个应用程序所需的环境信息的 Session 对象
				session = Session.getDefaultInstance(prop, new Authenticator() {
					public PasswordAuthentication getPasswordAuthentication() {
						// 发件人邮件用户名、授权码
						return new PasswordAuthentication(username, password);
					}
				});
			} else
				// 2.根据属性获得一个会话
				session = Session.getInstance(prop);
			// 开启Session的debug模式，这样就可以查看到程序发送Email的运行状态
			session.setDebug(debug);

			// 2、通过session得到transport对象
			Transport ts = session.getTransport();

			// 3、使用邮箱的用户名和授权码连上邮件服务器
			ts.connect(host, username, password);

			// 4、创建邮件
			MimeMessage message = new MimeMessage(session);
			message.setFrom(new InternetAddress(username)); // 发件人
			message.setRecipient(Message.RecipientType.TO, new InternetAddress(收件人)); // 收件人

			if (抄送 != null)
				message.setRecipient(Message.RecipientType.CC, new InternetAddress(抄送)); // 抄送
			message.setSubject(主题); // 邮件的标题
			message.setContent(内容, "text/html;charset=UTF-8");
			message.saveChanges();

			// 发送邮件
			ts.sendMessage(message, message.getAllRecipients());
			ts.close();
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
}